package com.qc.sys.web;


import com.github.pagehelper.PageInfo;
import com.qc.common.utils.StringUtils;
import com.qc.common.web.BaseController;
import com.qc.sys.entity.Dict;
import com.qc.sys.service.DictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 字典Controller
 */
@Controller
@RequestMapping(value = "sys/dict")
public class DictController extends BaseController {

	@Autowired
	private DictService dictService;
	
	@ModelAttribute
	public Dict get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return dictService.get(id);
		}else{
			return new Dict();
		}
	}
	
	@RequiresPermissions("sys:dict:view")
	@RequestMapping(value = {"list", ""})
	public String list(Dict dict, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<String> typeList = dictService.findTypeList();
		model.addAttribute("typeList", typeList);
		List<Dict> list = dictService.findListPage(dict);
        PageInfo<Dict> pageInfo = new PageInfo<>(list);
        model.addAttribute("page", pageInfo);
        model.addAttribute("dict", dict);
        setTitle(model, "字典列表");
		return "sys.dict.dictList";
	}

/*    @RequiresPermissions("sys:dict:view")
    @RequestMapping("list.json")
    @ResponseBody
    public Page listJson(Dict dict, HttpServletRequest request, HttpServletResponse response) {
        Page<Dict> page = dictService.findPage(new Page<Dict>(request, response), dict);
        return page;
    }*/



    @RequiresPermissions("sys:dict:view")
    @RequestMapping(value = "form")
    public String form(Dict dict, Model model) {
        model.addAttribute("dict", dict);
        if (StringUtils.isBlank(dict.getId())) {
            setTitle(model, "字典编辑");
        } else {
            setTitle(model, "字典添加");
        }
        return "sys.dict.dictForm";
    }

    @RequiresPermissions("sys:dict:edit")
    @RequestMapping(value = "save")//@Valid
    public String save(Dict dict, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request) {
        if (!beanValidator(model, dict)) {
            return form(dict, model);
        }
        dictService.save(dict);
        addMessage(redirectAttributes, "保存字典'" + dict.getLabel() + "'成功");
        redirectAttributes.addFlashAttribute("redirect", request.getContextPath()+"/sys/dict");
        return "redirect:/sys/dict";
    }

    @RequiresPermissions("sys:dict:edit")
    @RequestMapping(value = "delete")
    public String delete(Dict dict, RedirectAttributes redirectAttributes, HttpServletRequest request) {
        dictService.delete(dict);
        addMessage(redirectAttributes, "删除字典成功");
        redirectAttributes.addFlashAttribute("redirect", request.getContextPath()+"/sys/dict");
        return "redirect:/sys/dict";
    }


    @ResponseBody
    @RequestMapping(value = "listData")
    public List<Dict> listData(@RequestParam(required = false) String type) {
        Dict dict = new Dict();
        dict.setType(type);
        return dictService.findList(dict);
    }

}
